﻿using Petroleum.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.Drawing;
using System.Reflection;
using Petroleum.DB.Model;
using Spire.Doc.Collections;
using Spire.Doc.Interface;
using System.IO;

namespace Petroleum.Tool
{
    public class SpireDocUtil
    {
        public static string ImagesPath = AppDomain.CurrentDomain.BaseDirectory;
        /// <summary>
        /// 生成模板方法
        /// </summary>
        /// row 表示从第几行开始填充数据
        /// 每一步拆开填充数据
        /// <param name="data">数据源</param>
        /// <param name="docTemplatePath">模板地址</param>
        /// <param name="newTempletePath">生成模板地址</param>
        public static void CreateDoc(SessionDynamic data, string docTemplatePath, string newTempletePath)
        {
            try
            {
                Document document = new Document(docTemplatePath);
                //document.LoadFromFile(docTemplatePath, FileFormat.Docx2010);
                Section sec = document.Sections[2];//文档当中的节
                TableCollection tables = sec.Tables;//节中所有表格
                #region 替换钻机的表格
                if (!string.IsNullOrEmpty(Session.Team))
                {
                    IDocument gwdocs = new Document(Session.Team);
                    //使用该文档替换模板中的指定文本
                    document.Replace("{$GWdocs}", gwdocs, false, true);
                }
                #endregion
                #region 替换documents2个版本
                //Session.DocRequirements = AppConfig.TeamWordUrl + "docs.docx";
                if (!string.IsNullOrEmpty(Session.DocRequirements))
                {
                    IDocument docs = new Document(Session.DocRequirements);
                    document.Replace("{$docs}", docs, false, true);
                }
                #endregion
                #region step1
                if (data.BasicInf != null)
                {
                    ReplaceAllText(document, data.BasicInf);
                    ReplaceAllText(document, data.CasingProgram);
                    if (data.BasicInf.Wellhead != null)
                        ReplaceAllText(document, data.BasicInf.Wellhead);
                    if (data.BasicInf.TargetList != null)
                    {
                        for (int i = 0; i < data.BasicInf.TargetList.Count; i++)
                        {
                            ReplaceAllText(document, data.BasicInf.TargetList[i], i);
                        }
                        if (data.BasicInf.TargetList.Count == 1)
                        {
                            Table tablebasic = tables[0] as Table;
                            for (int j = 2; j < 6; j++)
                            {
                                tablebasic.ApplyVerticalMerge(j, 10, 11);
                            }
                        }
                    }
                }
                #endregion
                WordStep2(sec, data, document);
                if (data.FormationPressure != null)
                    ReplaceAllText(document, data.FormationPressure);
                if (data.WellTrajectory != null)
                    ReplaceAllText(document, data.WellTrajectory);//图片
                if (data.WellTrajectory != null && data.WellTrajectory.TrajectoryList != null && data.WellTrajectory.TrajectoryList.Count > 0)
                {
                    #region step3
                    TrajectoryTargetModel trajectory = new TrajectoryTargetModel();
                    Table table7 = SingleTable(sec, 7, data.WellTrajectory.TrajectoryList.Count);
                    int row7 = 1;
                    for (int i = 0; i < data.WellTrajectory.TrajectoryList.Count; i++)
                    {
                        var item = data.WellTrajectory.TrajectoryList[i];
                        TableRowData(item, table7, row7);
                        row7++;
                        if (i == data.WellTrajectory.TrajectoryList.Count - 1)
                        {
                            trajectory.tvd_Tra = item.TVD;
                            trajectory.md_Tra = item.MD;
                        }
                        if (!string.IsNullOrEmpty(item.IsTarget))
                        {
                            if (item.IsTarget.Contains("A"))
                            {
                                trajectory.tvdA = item.TVD;
                                trajectory.VA = item.VSec;
                            }

                            if (item.IsTarget.Contains("B"))
                            {
                                trajectory.tvdB = item.TVD;
                                trajectory.VB = item.VSec;
                            }
                            trajectory.azim = item.Azi;
                        }
                    }
                    ReplaceAllText(document, trajectory);
                    //Table table5 = sec.Tables[5] as Table;
                    //for (int i = 2; i < table5.Rows.Count; i++)
                    //{
                    //    TableRowData(trajectory, table5, i);
                    //}
                    #endregion
                }
                WordStep4(sec, data, document);
                WordStep789(sec, data, document);
                sec = document.Sections[5];
                WordStep10(sec, data);
                sec = document.Sections[8];
                WordStep11(sec, data);
                sec = document.Sections[10];
                WordStep12(sec, data);
                sec = document.Sections[13];
                WordStep13(sec, data);
                WordStep14(sec, data);
                if (File.Exists(ImagesPath + "ChartImg\\line.png"))
                {
                    ReplaceImg(ImagesPath + "ChartImg\\line.png", document);
                    StreamChuLi.DeleteDir(SpireDocUtil.ImagesPath + "ChartImg");
                }
                document.SaveToFile(newTempletePath + "CNPCNP" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".docx", FileFormat.Docx2010);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        static void ReplaceImg(string str, Document document)
        {
            TextSelection[] text = document.FindAllString("{$lineImg}", false, true);
            if (text == null) return;
            TextRange range = null;
            int index = 0;
            foreach (TextSelection seletion in text)
            {
                try
                {
                    DocPicture pic = new DocPicture(document);
                    pic.LoadImage(Image.FromFile(str));
                    range = seletion.GetAsOneRange();
                    index = range.OwnerParagraph.ChildObjects.IndexOf(range);
                    range.OwnerParagraph.ChildObjects.Insert(index, pic);
                    range.OwnerParagraph.ChildObjects.Remove(range);
                }
                catch (Exception e)
                {

                }
            }
        }
        /// <summary>
        /// 全文图片/字符串替换
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="document"></param>
        /// <param name="etity"></param>
        /// <param name="i"></param>
        static void ReplaceAllText<T>(Document document, T etity, int i = 0)
        {
            Type entityType = etity.GetType();
            PropertyInfo[] properties = entityType.GetProperties();
            TextRange range = null;
            int index = 0;
            foreach (var p in properties)
            {
                object value = p.GetValue(etity);
                if (value == null)
                {
                    value = "";
                    continue;
                }
                string propteryName = i == 0 ? "{$" + p.Name + "}" : "{$" + p.Name + i + "}";//Word模板中设定的需要替换的标签
                TextSelection[] text = document.FindAllString(propteryName, false, true);
                if (text == null) continue;
                if (propteryName.ToLower().Contains(i == 0 ? "img" : "img" + i) && value.ToString() != "")
                {
                    //设置高亮颜色
                    foreach (TextSelection seletion in text)
                    {
                        try
                        {
                            DocPicture pic = new DocPicture(document);
                            pic.LoadImage(Image.FromFile(value.ToString()));
                            range = seletion.GetAsOneRange();
                            index = range.OwnerParagraph.ChildObjects.IndexOf(range);
                            //pic.Height = 100;
                            //pic.Width = 100;
                            range.OwnerParagraph.ChildObjects.Insert(index, pic);
                            range.OwnerParagraph.ChildObjects.Remove(range);
                        }
                        catch (Exception ex)
                        {

                        }
                    }
                }
                else
                    document.Replace(propteryName, value.ToString(), false, true);//全局替换文本包括表格
                //foreach (TextSelection seletion in text)
                //{
                //    seletion.GetAsOneRange().CharacterFormat.HighlightColor = Color.Red;
                //}
            }
        }
        /// <summary>
        /// 生成表格模型
        /// 得到一个表格
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="sec"></param>
        /// <param name="etity">数据类型</param>
        /// <param name="tableindex">第几个表格</param>
        /// <param name="count">实际数据的条数</param>
        static Table SingleTable(Section sec, int tableindex = 0, int count = 0, int addindex = 0)
        {
            Table table = sec.Tables[tableindex] as Spire.Doc.Table;
            for (int i = 0; i < count - 1; i++)
            {
                //复制最后一行有多少条数据复制多少行
                if (addindex == 0)
                    addindex = table.Rows.Count - 1;
                TableRow row = table.Rows[addindex].Clone();
                table.Rows.Insert(addindex + i + 1, row);
                //table.Rows.Add(row);//默认将复制后的行添加在表格最下方
            }
            return table;
        }
        static Table complexTable(Section sec, int tableindex, List<Pressure> list, int addindex = 1)
        {
            Table table = sec.Tables[tableindex] as Table;
            int listcount = list.Where(n => n.elist == null || n.elist.Count <= 0).Count();
            //int elicount = list.Count - listcount;
            int currenteliindex = 0;
            int elicopyindex = 0;
            bool haselist = false;
            for (int i = 0; i < list.Count; i++)
            {
                //复制当前行
                var trow = list[i];
                if (trow.elist != null && trow.elist.Count > 0)//最后一行
                {
                    haselist = true;
                    addindex = table.Rows.Count - 1;
                    if (currenteliindex == 0)
                    {
                        elicopyindex = addindex;
                    }
                    for (int j = 0; j < trow.elist.Count - 1; j++)
                    {
                        TableRow row = table.Rows[elicopyindex].Clone();
                        table.Rows.Insert(addindex + 1, row);
                        currenteliindex++;
                    }
                }
                else//复制第二行
                {
                    if (listcount > 1)
                    {
                        if (addindex > 1)
                        {
                            TableRow row = table.Rows[1].Clone();
                            table.Rows.Insert(addindex + 1 + currenteliindex, row);
                            listcount--;
                        }
                        else
                        {
                            TableRow row = table.Rows[addindex].Clone();
                            table.Rows.Insert(i + 1, row);
                            listcount--;
                        }
                    }
                }
            }
            if (!haselist)//没有elist集合就删除最后一行
            {
                table.Rows.RemoveAt(table.Rows.Count - 1);
            }
            return table;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entity"></param>
        /// <param name="table"></param>
        /// <param name="rowindex"></param>
        /// <param name="hasmark">是否要保留表格中原来的数据，还是直接替换</param>
        /// <param name="i">标签多余的编号</param>
        static void TableRowData<T>(T entity, Table table, int rowindex = 0, int hasmark = 0, int j = 0, int Paragraphsindex = 0)
        {
            Type entityType = entity.GetType();
            PropertyInfo[] properties = entityType.GetProperties();
            foreach (var p in properties)
            {
                object value = p.GetValue(entity);
                if (value == null)
                {
                    value = "";
                }
                string propteryName = j == 0 ? "{$" + p.Name + "}" : "{$" + p.Name + j + "}";//Word模板中设定的需要替换的标签
                for (int i = 0; i < table.Rows[rowindex].Cells.Count; i++)
                {
                    var str = table.Rows[rowindex].Cells[i].Paragraphs[Paragraphsindex].Text;
                    if (propteryName.ToLower().Contains(j == 0 ? "img" : "img" + j) && str.Contains(propteryName))
                    {
                        table.Rows[rowindex].Cells[i].Paragraphs[0].Text = "";
                        if (value != null && value.ToString() != "")
                        {
                            try
                            {
                                var img = Image.FromFile(value.ToString());
                                if (img != null)
                                    table.Rows[rowindex].Cells[i].Paragraphs[0].AppendPicture(img);
                            }
                            catch (Exception e)
                            {

                            }
                            //var widthEmus = (int)(400.0 * 9525);
                            //var heightEmus = (int)(300.0 * 9525);
                            //pd.Height = 100;
                            //pd.Width = 100;
                        }
                    }
                    else if (str.Contains(propteryName))
                    {
                        if (hasmark == 1)
                        {
                            string strtmp = table.Rows[rowindex].Cells[i].Paragraphs[0].Text;
                            strtmp = strtmp.Substring(0, strtmp.LastIndexOf("：") + 1);
                            table.Rows[rowindex].Cells[i].Paragraphs[0].Text = strtmp + value.ToString();
                        }
                        else
                            table.Rows[rowindex].Cells[i].Paragraphs[0].Text = value.ToString();
                    }
                }
            }
        }
        #region step
        static void WordStep789(Section sec, SessionDynamic data, Document document)
        {
            try
            {
                if (data.DepthRangeModelList != null && data.DepthRangeModelList.Count > 0)
                {
                    #region step7
                    Table table17 = SingleTable(sec, 17, data.DepthRangeModelList.Count);
                    int row7 = 1;
                    foreach (var item in data.DepthRangeModelList)
                    {
                        TableRowData(item, table17, row7);
                        row7++;
                    }
                    int row_2 = 3;
                    if (data.DepthRangeModelList.Count > 1)
                    {
                        sec = document.Sections[3];//文档当中的节
                                                   //tables = sec.Tables;//节中所有表格
                        Table table_1 = SingleTable(sec, 0, data.DepthRangeModelList.Count);
                        TableRowData(data.DepthRangeModelList[1], table_1, row_2);
                        row_2++;
                    }
                    if (data.DepthRangeModelList.Count > 2)
                    {
                        sec = document.Sections[4];
                        row_2 = 3;
                        Table table_0 = SingleTable(sec, 0, data.DepthRangeModelList.Count);
                        TableRowData(data.DepthRangeModelList[2], table_0, row_2);
                        row_2++;
                    }
                    #endregion
                    #region step8
                    sec = document.Sections[5];
                    if (data.DepthWellModelList != null && data.DepthWellModelList.Count > 0)
                    {
                        Table table_well = sec.Tables[0] as Table;
                        for (int i = 0; i < 8; i++)//一共循环7行
                        {
                            for (int j = 0; j < data.DepthWellModelList.Count; j++)
                            {
                                TableRowData(data.DepthWellModelList[j], table_well, i, 0, j);
                            }
                        }
                    }
                    #endregion
                    #region step9
                    if (data.BitTypeDes != null && data.BitTypeDes.Count > 0)
                    {
                        Table tablebittyps_1 = SingleTable(sec, 1, data.BitTypeDes.Count);
                        int rowbittyps_1 = 2;
                        foreach (var item in data.BitTypeDes)
                        {
                            TableRowData(item, tablebittyps_1, rowbittyps_1);
                            rowbittyps_1++;
                        }
                    }
                    #endregion
                }
            }
            catch (Exception e)
            {

            }
        }
        static void WordStep2(Section sec, SessionDynamic data, Document document)
        {
            try
            {
                if (data.FormationPressure == null) return;
                if (data.FormationPressure.list != null && data.FormationPressure.list.Count > 0)
                {
                    var list = data.FormationPressure.list.OrderBy(n => n.ordernum).ToList();
                    #region step2
                    List<PressureABC> pressureABCs = new List<PressureABC>();
                    //获取表格
                    Table table = SingleTable(sec, 1, list.Count);
                    int row = 2;
                    for (int i = 0; i < list.Count; i++)
                    {
                        var item = list[i];
                        pressureABCs.Add(new PressureABC() { TVD_ = item.TVD });
                        TableRowData(item, table, row);
                        row++;
                    }
                    for (int i = 0; i < pressureABCs.Count; i++)
                    {
                        ReplaceAllText(document, pressureABCs[i], i);
                    }
                    Table table2 = complexTable(sec, 2, list);
                    int row2 = 1;

                    foreach (var item in list)
                    {
                        TableRowData(item, table2, row2);
                        if (item.elist != null && item.elist.Count > 0)
                        {
                            int row3 = row2;
                            foreach (var eli in item.elist)
                            {
                                TableRowData(item, table2, row3);
                                TableRowData(eli, table2, row3);
                                row3++;
                            }
                            if (item.elist.Count > 1)
                            {
                                //垂直合并（合并第1列的第3、4个单元格）
                                table2.ApplyVerticalMerge(0, row2, row3 - 1);
                                table2.ApplyVerticalMerge(1, row2, row3 - 1);
                                table2.ApplyVerticalMerge(4, row2, row3 - 1);
                                table2.ApplyVerticalMerge(5, row2, row3 - 1);
                                table2.ApplyVerticalMerge(6, row2, row3 - 1);
                                table2.ApplyVerticalMerge(7, row2, row3 - 1);
                            }
                            row2 += item.elist.Count;
                        }
                        row2++;
                    }
                    #region step5
                    int row10 = 1;
                    Table table10 = SingleTable(sec, 10, list.Count, row10);
                    table10.ApplyVerticalMerge(2, 1, list.Count);
                    table10.ApplyVerticalMerge(3, 1, list.Count);
                    //水平合并（合并第1行的第1、2、3、4个单元格）
                    //table.ApplyHorizontalMerge(0, 0, 3);
                    //垂直合并（合并第1列的第3、4个单元格）
                    TableRowData(data.FormationPressure, table10, row10);
                    foreach (var item in list)
                    {
                        TableRowData(item, table10, row10);
                        row10++;
                    }
                    #endregion
                }
                #endregion
            }
            catch (Exception ex)
            {

            }
        }
        static void WordStep4(Section sec, SessionDynamic data, Document document)
        {
            try
            {
                if (data.CasingProgram != null && data.CasingProgram.CasingList != null && data.CasingProgram.CasingList.Count > 0)
                {
                    #region step4
                    int row6 = 1;//从第1行开始写数据
                    Table table6 = SingleTable(sec, 6, data.CasingProgram.CasingList.Count, row6);
                    foreach (var item in data.CasingProgram.CasingList)
                    {
                        TableRowData(item, table6, row6);
                        row6++;
                    }
                    int row9 = 1;
                    Table table9 = SingleTable(sec, 9, data.CasingProgram.CasingList.Count, row9);
                    foreach (var item in data.CasingProgram.CasingList)
                    {
                        TableRowData(item, table9, row9);
                        row9++;
                    }
                    int row11 = 1;
                    Table table11 = SingleTable(sec, 11, data.CasingProgram.CasingList.Count, row11);
                    foreach (var item in data.CasingProgram.CasingList)
                    {
                        TableRowData(item, table11, row11);
                        row11++;
                    }
                    #endregion
                    #region step6
                    int tableindex = 13;
                    //主表信息从第2行开始填充
                    int row12_6 = 6;//BHA表信息从第1行开始填充
                    int row12_9 = 9;//Drilling parameters：表信息从第1行开始填充
                    for (int i = 0; i < data.CasingProgram.CasingList.Count; i++)
                    {
                        Table table12 = sec.Tables[tableindex] as Table;
                        var item = data.CasingProgram.CasingList[i];
                        for (int j = 1; j < 5; j++)//从第一行开始，供替换4行的数据
                        {
                            TableRowData(item, table12, j, 1);
                        }
                        table12 = SingleTable(sec, tableindex, item.DrillingParametersList.Count, row12_9);
                        table12 = SingleTable(sec, tableindex, item.BHAList.Count, row12_6);
                        //row12++;
                        tableindex++;
                    }
                    int tableindexdetail = 13;
                    for (int i = 0; i < data.CasingProgram.CasingList.Count; i++)
                    {
                        row12_6 = 6;
                        row12_9 = 9;
                        Table table12 = sec.Tables[tableindexdetail] as Table;
                        var item = data.CasingProgram.CasingList[i];
                        if (item.BHAList != null && item.BHAList.Count > 0)
                        {
                            foreach (var itembha in item.BHAList)
                            {
                                TableRowData(itembha, table12, row12_6);
                                row12_6++;
                            }
                        }
                        row12_9 = 3 + row12_6 - 1;
                        if (item.DrillingParametersList != null && item.DrillingParametersList.Count > 0)
                        {
                            foreach (var itembha in item.DrillingParametersList)
                            {
                                if (!string.IsNullOrEmpty(itembha.WOBkN))
                                {
                                    TableRowData(itembha, table12, row12_9);
                                    row12_9++;
                                }
                            }
                        }
                        tableindexdetail++;
                    }
                    Table casingimg = sec.Tables[16] as Table;
                    TableRowData(data.CasingProgram, casingimg, 0);
                    #endregion
                    #region 补充
                    sec = document.Sections[11];
                    Table table1 = sec.Tables[0] as Table;
                    int row1 = 1;
                    for (int i = 1; i < data.CasingProgram.CasingList.Count; i++)
                    {
                        TableRowData(data.CasingProgram.CasingList[i], table1, row1, 0, i);
                        row1++;
                    }
                    if (data.DepthWellModelList != null && data.DepthWellModelList.Count > 0)
                    {
                        Table table2 = sec.Tables[1] as Table;
                        TableRowData(data.DepthWellModelList[0], table2, 10);
                    }
                    #endregion
                }
            }
            catch (Exception e)
            {

            }
        }
        static void WordStep10(Section sec, SessionDynamic data)
        {
            try
            {
                #region step10
                if (data.Hydraulic != null && data.Hydraulic.hydraulics != null && data.Hydraulic.hydraulics.Count > 0)
                {

                    Table table_hy = SingleTable(sec, 2, data.Hydraulic.hydraulics.Count);
                    int rowbittyps_1 = 2;
                    foreach (var item in data.Hydraulic.hydraulics)
                    {
                        TableRowData(item, table_hy, rowbittyps_1);
                        rowbittyps_1++;
                    }
                    Table table_hyimg = sec.Tables[3] as Table;
                    TableRowData(data.Hydraulic, table_hyimg, 0);
                }
                #endregion
            }
            catch (Exception e)
            {

            }
        }
        static void WordStep11(Section sec, SessionDynamic data)
        {
            try
            {
                if (data.MPAList != null && data.MPAList.Count > 0)
                {
                    Table table = sec.Tables[0] as Table;
                    for (int i = 2; i < 7; i++)//一共循环5行
                    {
                        for (int j = 0; j < data.MPAList.Count; j++)
                        {
                            TableRowData(data.MPAList[j], table, i, 0, j);
                        }
                    }
                }
                if (data.MPADetailList != null && data.MPADetailList.Count > 0)
                {
                    Table table = sec.Tables[1] as Table;
                    for (int i = 1; i < 3; i++)//一共循环2行
                    {
                        for (int j = 0; j < data.MPADetailList.Count; j++)
                        {
                            TableRowData(data.MPADetailList[j], table, i, 0, j);
                        }
                    }
                }
            }
            catch (Exception e)
            {

            }
        }
        static void WordStep12(Section sec, SessionDynamic data)
        {
            #region step12
            if (data.CasingProgram != null && data.CasingProgram.CasingList != null && data.CasingProgram.CasingList.Count > 0)
            {
                Table table = sec.Tables[0] as Table;
                for (int i = 2; i < 5; i++)//一共循环3行
                {
                    for (int j = 0; j < data.CasingProgram.CasingList.Count; j++)
                    {
                        TableRowData(data.CasingProgram.CasingList[j], table, i, 0, j);
                    }
                }
            }
            #endregion
        }
        static void WordStep13(Section sec, SessionDynamic data)
        {
            try
            {
                List<SlurryModel> list = new List<SlurryModel>();
                #region step13
                if (data.slurries != null && data.slurries.Count > 0)
                {
                    Table table = SingleTable(sec, 0, data.CasingProgram.CasingList.Count);
                    int addrowindex = 1;
                    foreach (var item in data.CasingProgram.CasingList)
                    {
                        var slu = data.slurries.Where(n => n.SpudNo == item.SpudNo).ToList();
                        SlurryModel model = slu.FirstOrDefault();
                        model.CementExcess = slu.Sum(n => n.CementExcess);
                        model.SlurryVolume = slu.Sum(n => n.SlurryVolume);
                        TableRowData(model, table, addrowindex);
                        addrowindex++;
                    }
                }
                #endregion
            }
            catch (Exception e)
            {

            }
        }
        static void WordStep14(Section sec, SessionDynamic data)
        {
            try
            {
                #region step12
                if (data.ProgresPlanList != null && data.ProgresPlanList.Count > 0)
                {
                    data.ProgresPlanList.Add(new ProgresPlan() { Item = "Total", Day = data.ProgresPlanList.Max(n => n.AccDay) });
                    int addrowindex = 2;
                    Table table = SingleTable(sec, 1, data.ProgresPlanList.Count);
                    foreach (var item in data.ProgresPlanList)
                    {
                        //item.Interval = item.Interval + "-" + item.IntervalTo;
                        TableRowData(item, table, addrowindex);
                        addrowindex++;
                    }
                    table.ApplyHorizontalMerge(data.ProgresPlanList.Count + 1, 1, 2);
                }
                #endregion
            }
            catch (Exception e)
            {

            }
        }
        #endregion
        #region 暂时用不着
        static Table complexTable(Section sec, List<Pressure> list, int tableindex = 0, int addindex = 0)
        {
            Table table = sec.Tables[tableindex] as Spire.Doc.Table;
            for (int i = 0; i < list.Count - 1; i++)
            {
                //复制最后一行有多少条数据复制多少行
                if (addindex == 0)
                    addindex = table.Rows.Count - 1;
                TableRow row = table.Rows[addindex].Clone();
                table.Rows.Insert(addindex + i + 1, row);
                //table.Rows.Add(row);//默认将复制后的行添加在表格最下方
            }
            return table;
        }
        static void Step2TableRowData(Pressure entity, Table table, int rowindex = 0, int hasmark = 0)
        {
            Type entityType = entity.GetType();
            PropertyInfo[] properties = entityType.GetProperties();
            foreach (var p in properties)
            {
                object value = p.GetValue(entity);
                if (value == null)
                {
                    value = "";
                }
                string propteryName = "{$" + p.Name + "}";//Word模板中设定的需要替换的标签
                for (int i = 0; i < table.Rows[rowindex].Cells.Count; i++)
                {
                    var str = table.Rows[rowindex].Cells[i].Paragraphs[0].Text;
                    if (str.Contains(propteryName))
                    {
                        table.Rows[rowindex].Cells[i].Paragraphs[0].Text = value.ToString();
                    }
                    if (entity.elist != null && entity.elist.Count > 0)
                    {
                        TableCell cell = table.Rows[rowindex].AddCell(true);
                        cell.Width = table[0, 0].Width;
                        cell.CellWidthType = table[0, 0].CellWidthType;
                    }
                }
            }
        }
        #endregion
    }
}
